package com.movie.ticketing.paymentservice.controller;

import com.movie.ticketing.paymentservice.entity.Payment;
import com.movie.ticketing.paymentservice.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/payment")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    // 创建支付订单
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> createPaymentOrder(@RequestBody Payment payment) {
        Map<String, Object> response = new HashMap<>();
        try {
            Payment savedPayment = paymentService.savePayment(payment);
            response.put("code", 200);
            response.put("message", "创建支付订单成功");
            response.put("data", savedPayment);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("code", 400);
            response.put("message", "创建支付订单失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取所有支付记录
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllPayments() {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Payment> payments = paymentService.getAllPayments();
            response.put("code", 200);
            response.put("message", "查询所有支付记录成功");
            response.put("data", payments);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    // 获取支付状态
    @GetMapping("/status/{id}")
    public ResponseEntity<Map<String, Object>> getPaymentStatus(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        try {
            Payment payment = paymentService.getPaymentById(id);
            if (payment != null) {
                response.put("code", 200);
                response.put("message", "获取支付状态成功");
                response.put("data", payment);
                return ResponseEntity.ok(response);
            } else {
                response.put("code", 404);
                response.put("message", "支付记录未找到");
                return ResponseEntity.status(404).body(response);
            }
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "获取支付状态失败：" + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    // 处理支付回调（模拟第三方支付回调）
    @PostMapping("/callback")
    public ResponseEntity<Map<String, Object>> handlePaymentCallback(@RequestBody Payment paymentCallbackData) {
        Map<String, Object> response = new HashMap<>();
        try {
            Payment updatedPayment = paymentService.updatePayment(paymentCallbackData.getId(), paymentCallbackData);
            response.put("code", 200);
            response.put("message", "支付回调处理成功");
            response.put("data", updatedPayment);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("code", 400);
            response.put("message", "支付回调处理失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 退款
    @PostMapping("/refund/{id}")
    public ResponseEntity<Map<String, Object>> refundPayment(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        try {
            paymentService.deletePayment(id);
            response.put("code", 200);
            response.put("message", "支付退款成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "支付退款失败：" + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }
}